package com.amor.app

import androidx.annotation.ColorRes
import androidx.databinding.ViewDataBinding
import com.amor.demo.web1.R
import com.amor.net.ImageLoader

import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.hy.frame.mvvm.base.BaseViewModel
import com.hy.frame.ui.LoadingDialog
import com.hy.frame.util.ToastUtil


/**
 * title BaseActivity
 * author HeYan
 * date 2021/9/25
 * desc
 */
abstract class BaseActivity<DB : ViewDataBinding, VM : BaseViewModel> : com.hy.frame.mvvm.base.BaseActivity<DB, VM>() {

    open fun isFullscreen(): Boolean = false

    override fun isTranslucentStatus(): Boolean = false

    @ColorRes
    open fun getStatusBarColor(): Int = R.color.colorPrimary //默认状态栏颜色白色

    @ColorRes
    open fun getNavigationBarColor(): Int = R.color.white //默认导航栏颜色白色

    protected val imgLoader by lazy { ImageLoader(this) }

    private var loadingDialog: LoadingDialog? = null

    override fun initLayout() {
        super.initLayout()
        initStatusBar()
    }

    /**
     * 初始化状态栏 需要放在UI初始化后
     */
    open fun initStatusBar() {
        when {
            isFullscreen() -> {
                ImmersionBar.with(this).transparentBar().hideBar(BarHide.FLAG_HIDE_BAR).statusBarDarkFont(getStatusBarColor() == R.color.white).init()
            }
            isTranslucentStatus -> {
                ImmersionBar.with(this).transparentStatusBar().navigationBarColor(getNavigationBarColor()).statusBarDarkFont(getStatusBarColor() == R.color.white)
                    .init()
            }
            else -> {
                ImmersionBar.with(this).statusBarColor(getStatusBarColor()).navigationBarColor(getNavigationBarColor())
                    .statusBarDarkFont(getStatusBarColor() == R.color.white).fitsSystemWindows(true).init()
            }
        }
    }

    override fun showLoading(msg: String?) {
        if (loadingDialog == null)
            loadingDialog = LoadingDialog(this)
        loadingDialog?.show(msg)
    }

    override fun dismissLoading() {
        loadingDialog?.dismiss()
        screenOrientation
    }

    override fun showToast(msg: String?) {
        ToastUtil.show(this, msg)
    }

}